package com.fast.controller;

import com.fast.controller.login.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @author hujw
 * @since 12月22号-0022
 */
@Slf4j
@Component
public class MyListener {

    @EventListener
    public void listenerApplicationStarted(ApplicationStartedEvent event) {
        log.info("应用启动完成");
    }

    @EventListener({ApplicationEnvironmentPreparedEvent.class})
    public void listenerApplicationEnv() {
        //实际测试，没有监听到，后面说明原理
        log.info("监听到了环境准备完成事件");
    }

    //监听多个事件
    @EventListener({ApplicationReadyEvent.class, ApplicationStartedEvent.class})
    public void listenerApplication() {
        log.info("监听到了多个事件");
    }

    //自己发布了一个Person事件，Person并没有继承ApplicationEvent
    @EventListener
    public void myCustomListener(User person) {
        log.info("监听到自己发布的事件,{}", person);
    }

    //只有Person事件中name属性值为csdn时才接收到
    @EventListener(condition = "#person.name == 'csdn'")
    public void myCustomListener2(User person) {
        log.info("SpEL表达式监听到自己发布的事件,{}", person);
    }
}
